﻿Imports MySql.Data.MySqlClient
Imports System.Windows.Forms

Public Class SuperComboBox
    Inherits Windows.Forms.ComboBox

    Public Event OnInsert(ByRef inCbo As SuperComboBox)
    Public Event OnModify(ByRef inCbo As SuperComboBox, ByVal intID As Integer)

    Private strQuery As String
    Private strNameField As String
    Private strConnectionString As String
    Private blnEnableFunctions As Boolean = True


    Public Sub New()
        Me.DropDownStyle = Windows.Forms.ComboBoxStyle.DropDownList
    End Sub

#Region "Public functions"

    Public Property EnableFunctions As Boolean
        Get
            Return blnEnableFunctions
        End Get
        Set(ByVal value As Boolean)
            blnEnableFunctions = value
            If blnEnableFunctions Then
                Dim oToolTip As New Windows.Forms.ToolTip
                oToolTip.SetToolTip(Me, "F5: Insert element" & vbCrLf & "F6: Modify element")
                oToolTip = Nothing
            Else
                Dim oToolTip As New Windows.Forms.ToolTip
                oToolTip.SetToolTip(Me, "")
                oToolTip = Nothing
            End If
        End Set
    End Property

    Public Function Ricarica(ByVal inIDSelezionato As Integer) As Boolean
        If Me.Load Then
            If inIDSelezionato > 0 Then
                Me.SelectedValue = inIDSelezionato
            End If
            Return True
        Else
            Return False
        End If
    End Function

    Public Function Load(ByVal inQuery As String, ByVal inNameField As String, ByVal inConnectionString As String) As Boolean
        If inQuery <> "" Then strQuery = inQuery
        If inNameField <> "" Then strNameField = inNameField
        If inConnectionString <> "" Then strConnectionString = inConnectionString

        Return Me.Load
    End Function

    Public Function IsItemSelected() As Boolean
        Try
            If Me.SelectedValue IsNot Nothing AndAlso IsNumeric(Me.SelectedIndex) AndAlso IsNumeric(Me.SelectedValue) Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            Base.ShowError.Show("Load data", Me.Name, "IsItemSelected", ex)
            Return False
        End Try
    End Function

#End Region

#Region "Private functions"

    Private Function Load() As Boolean
        Try
            Me.Tag = "Loading"
            Me.DataSource = Nothing
            Me.Items.Clear()

            Using daAdapt As New MySqlDataAdapter(strQuery, strConnectionString)
                Using dtTable As New DataTable("Table")
                    daAdapt.Fill(dtTable)
                    Me.DataSource = dtTable.Copy
                    Me.DisplayMember = strNameField
                    Me.ValueMember = "ID"
                End Using
            End Using
            Me.SelectedIndex = -1

            Return True
        Catch ex As Exception
            Base.ShowError.Show("Load", Me.Name, "Ricarica", ex)
            Return False
        Finally
            Me.Tag = ""
        End Try
    End Function

    Private Sub SuperComboBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyCode
            Case Windows.Forms.Keys.Return
                e.Handled = True
                Windows.Forms.SendKeys.Send("{TAB}")
                e.SuppressKeyPress = True
            Case Windows.Forms.Keys.F5
                If blnEnableFunctions Then RaiseEvent OnInsert(Me)
            Case Windows.Forms.Keys.F6
                If blnEnableFunctions Then RaiseEvent OnModify(Me, If(IsNumeric(Me.SelectedValue), Me.SelectedValue, 0))
        End Select
    End Sub

    Private Sub me_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.DropDown
        Try
            If Me.DataSource Is Nothing Then Return
            If Me.DataSource.GetType Is GetType(DataTable) Then
                Dim senderComboBox As SuperComboBox = DirectCast(sender, SuperComboBox)
                Dim width As Integer = senderComboBox.DropDownWidth
                Dim g As Graphics = senderComboBox.CreateGraphics()
                Dim font As Font = senderComboBox.Font
                Dim vertScrollBarWidth As Integer = If((senderComboBox.Items.Count > senderComboBox.MaxDropDownItems), SystemInformation.VerticalScrollBarWidth, 0)

                Dim newWidth As Integer
                For Each lRow As DataRow In CType(Me.DataSource, DataTable).Rows
                    Dim strNome As String = lRow(Me.DisplayMember).ToString
                    newWidth = CInt(g.MeasureString(strNome, font).Width) + vertScrollBarWidth
                    If width < newWidth Then
                        width = newWidth
                    End If
                Next
                senderComboBox.DropDownWidth = width
            End If
        Catch ex As Exception
            Base.ShowError.Show("Load", Me.Name, "me_DropDown", ex)
        End Try

    End Sub

#End Region

End Class
